Kotlin Class must have Constructor
● Default Constructor is created by compiler if you don't explicitly declare any Constructor
● Primary Constructor is declared before the body and its implementation is given inside init{} block
○ there can be only one Primary Constructor
○ you don't have to declare one but if there is one, Secondary Constructors must eventually call it
● Secondary Constructor is declared inside the Body
○ you can have multiple Secondary Constructors with or without Primary Constructor
○ if there is Primary Constructor, Secondary Constructors must call it constructor(name: String) : this(name, 20) {
Input parameters of Primary Constructor are declared after Class Name and before the Class Body.
● If you declare them with var or val they automatically become Class Properties
You can reference them with their name or inside the string with $name or ${name} (for complex expressions)
So just like in the Function you don't need to redeclare them inside the Class.
Additionally you can specify them to be private (like you can with any Class Property).
● Without var or val they are just Constructor Parameters (and not Class Properties.)
Then you can only reference them inside init{} blocks and during Properties initialization (in the same way as above).
Actual implementation of Primary Constructor can be defined inside multiple init{} blocks and Properties Declarations
which are all executed in the given order (inside the Class Body as if representing a Function Body).
Using constructor keyword is optional. Without it declaration syntax looks more like that of a Function.
Primary Constructor Syntax
class Person constructor(public var name: String, age: Int) { ... init{} ... init{} ... } //Optional
class Person ( var name: String, val age: Int) { ... init{} ... init{} ... } //Properties
class Person ( name: String, age: Int) { ... init{} ... init{} ... } //Parameters
Just Primary Constructor
//===========================================================================================================
// CLASS: Person
//===========================================================================================================
class Person constructor(name: String = "", public var age: Int = 0) {
//PRIMARY CONSTRUCTOR IMPLEMENTATION.
init { println("name is $name") } //name is only Constructor Parameter
//DECLARE & INITIALIZE PROPERTIES.
var greet : String = "Hello $name" //Use Constructor Parameter name to initialize Property
//PRIMARY CONSTRUCTOR IMPLEMENTATION.
init { println("age is ${age}") } //age is both Constructor Parameter & Class Property
//DECLARE METHODS.
fun sayHello() {
println("$greet. You are ${age} years old") //Can't reference name since it is not Property
}
}
//===========================================================================================================
// FUNCTION: main
//===========================================================================================================
fun main() {
var john = Person("John", 20) //Create Class Instance by calling its constructor() Method
john.sayHello() //Hello John. You are 20 years old
print(john.age) //20 Because of var, age Parameter was created as Class Property
}